#include "common.h"
#include "object.h"

#include "asset.h"
#include "mesh.h"
#include "effect.h"
#include "body.h"

#include "assetmanager.h"

std::map< std::string, Asset* >		AssetManager::_assets;
std::map< std::string, Body* >		AssetManager::_bodys;
std::map< std::string, Mesh* >		AssetManager::_meshes;
std::map< std::string, Effect* >	AssetManager::_effects;

AssetManager::AssetManager()
{
}

AssetManager::~AssetManager()
{
}

bool AssetManager::Find( const std::string& name )
{
	if( _assets.find( name ) == _assets.end() )
	{
		return false;
	}
	return true;
}

Asset* AssetManager::GetAsset( const std::string& name )
{
	std::map< std::string, Asset* >::iterator it = _assets.find( name );
	if( it == _assets.end() )
	{
		return NULL;
	}

	return it->second;
}

Body* AssetManager::GetBody( const std::string& name )
{
	std::map< std::string, Body* >::iterator it = _bodys.find( name );
	if( it == _bodys.end() )
	{
		return NULL;
	}
	return it->second;
}

Effect* AssetManager::GetEffect( const std::string& name )
{
	std::map< std::string, Effect* >::iterator it = _effects.find( name );
	if( it == _effects.end() )
	{
		return NULL;
	}

	return it->second;
}

Mesh* AssetManager::GetMesh( const std::string& name )
{
	std::map< std::string, Mesh* >::iterator it = _meshes.find( name );
	if( it == _meshes.end() )
	{
		return NULL;
	}

	return it->second;
}

void AssetManager::Register( const std::string& name, Asset* pAsset )
{
	if( Find( name ) || pAsset == NULL )
	{
		return;
	}

	_assets[ name ] = pAsset;

	if( pAsset->IsA( Body::GetTypeInfo() ) )
	{
		_bodys[ name ] = ( Body* )pAsset;
	}
	if( pAsset->IsA( Effect::GetTypeInfo() ) )
	{
		_effects[ name ] = ( Effect* )pAsset;
	}
	if( pAsset->IsA( Mesh::GetTypeInfo() ) )
	{
		_meshes[ name ] = ( Mesh* )pAsset;
	}

}

void AssetManager::Unregister( const std::string& name )
{
}

void AssetManager::UnregisterAll()
{
	if( _assets.empty() )
	{
		return;
	}

	std::map< std::string, Asset* >::iterator it = _assets.begin();
	for( ; it != _assets.end(); ++it )
	{
		delete it->second;
	}
}